package org.twnmayberry.ejb;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.twndmv.ejb.DmvTestUtilRemote;
import org.twnmayberry.bl.MayberryTestUtil;
import org.twnmayberry.blimpl.MayberryTestUtilImpl;

@Stateless
public class MayberryTestUtilEJB implements MayberryTestUtilRemote {
	private static Log log = LogFactory.getLog(MayberryTestUtilEJB.class);
	
	@PersistenceContext(unitName = "mayberryEntity")
	private EntityManager em;
	
	private MayberryTestUtil testUtil;

	@EJB(lookup="ejb:twnDmvEAR/twnDmvEJB/DmvTestUtilEJB!org.twndmv.ejb.DmvTestUtilRemote")
	private DmvTestUtilRemote dmvTestUtil;
	
	@PostConstruct
	public void init() {
		testUtil = new MayberryTestUtilImpl();
		testUtil.setEntityManager(em);
	}
	
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public void resetAll() {
		log.debug("Resetting mayberry database...");
		testUtil.resetAll();
		dmvTestUtil.resetAll();
	}
	
}
